var fs = require('fs');
var config = require('../../../sdp_config.json');
var WorkspaceProxy = require('../../../proxy/workspace.js');
var ProjectProxy = require('../../../proxy/project.js');
var _ = require('lodash');
var logger = require('log4js').getLogger();
var request = require('request');
var readline = require('readline');
var waterfall = require('async-waterfall');
var removeDir = require('../../../common/removeDir.js');
var uuid = require('uuid');


/**
 * 根据workspace_id获取工作空间依赖
 * 和项目依赖比较之后信息
 * @param req
 * @param res
 * @param next
 */
exports.getDependencies = function (req, res, next) {
    var work_id = req.params.work_id;
    WorkspaceProxy.getWorkspaceById(work_id, function (err, workspace) {
        if (err) {
            logger.error(err);
            res.status(500);
            return res.format_json(null, 500, err);
        }
        ProjectProxy.getProjectById(workspace.project_id, function (err, project) {
            if (err) {
                logger.error(err);
                res.status(500);
                return res.format_json(null, 500, err);
                ;
            }
            compareDependencies(workspace.dependencies,
                project.dependencies, function (data) {
                    res.send(data);
                });
        });
    });
}

function compareDependencies (work_dependencies, project_dependencies, call_back) {

    var invalidArray = _.remove(project_dependencies, function (n) {
        return n.status <= 0;
    });

    var unloadArray = _.remove(project_dependencies, function (n) {
        return n.status == 1;
    });

    var sameArray = _.intersectionWith(project_dependencies, work_dependencies, _.isEqual);//交集,完全相同的依赖

    var diffProjArray = _.xorWith(project_dependencies, sameArray, _.isEqual);//差集
    var diffWorkArray = _.xorWith(work_dependencies, sameArray, _.isEqual);//差集

    var behindArray = _.intersectionWith(diffProjArray, diffWorkArray, function (obj1, obj2) {//版本落后
        return obj1.name == obj2.name;
    });

    var newArray = _.xorWith(diffProjArray, behindArray, _.isEqual);//新的产品，还未依赖

    for (var i = 0; i < newArray.length; i++) {
        newArray[i].flag = config.compare_flag.new;
    }
    for (var i = 0; i < unloadArray.length; i++) {
        unloadArray[i].flag = config.compare_flag.unload;
    }
    for (var i = 0; i < sameArray.length; i++) {
        sameArray[i].flag = config.compare_flag.same;
    }
    for (var i = 0; i < behindArray.length; i++) {
        behindArray[i].flag = config.compare_flag.behind;
    }
    for (var i = 0; i < invalidArray.length; i++) {
        invalidArray[i].flag = config.compare_flag.invalid;
    }
    var returnArray = newArray.concat(sameArray, behindArray, invalidArray, unloadArray);
    returnArray = returnArray.sort(getSortFun('asc', 'name'));
    call_back(returnArray);
};
function getSortFun(order, sortBy) {
    var ordAlpah = (order == 'asc') ? '>' : '<';
    var sortFun = new Function('a', 'b', 'return a.' + sortBy + ordAlpah + 'b.' + sortBy + '?1:-1');
    return sortFun;
};
exports.getSortFun = function (order, sortBy) {
    var ordAlpah = (order == 'asc') ? '>' : '<';
    var sortFun = new Function('a', 'b', 'return a.' + sortBy + ordAlpah + 'b.' + sortBy + '?1:-1');
    return sortFun;
};
